﻿using AutoMapper;
using Domain.Event;
using Domain.IRepositorys;
using Domain.Model;
using Infrastructure.DomainEvent;
using Infrastructure.UniOfWorkBase;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Application.Event
{
    /// <summary>
    /// 全局收支记录
    /// </summary>
    public class FinancialEventHandler : IDomainEventHandler<FinancialEvent>
    {
        public IFinancialRepository financialRepository { get; set; }
        public IEFUnitOfWork efUnitOfWork { get; set; }
        public override Task HandleAysnc(FinancialEvent domainEvent, CancellationToken cancellationToken = default)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<FinancialEvent, FinancialAggregate>().ForMember(i=>i.ID,opt=>opt.Ignore());

                cfg.SourceMemberNamingConvention = new PascalCaseNamingConvention();
                cfg.DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
            });
            var mapper = config.CreateMapper();

            FinancialAggregate financialAggregate = mapper.Map<FinancialAggregate>(domainEvent);

            financialRepository.Insert(financialAggregate);

            efUnitOfWork.Commit();

            return Task.CompletedTask;
        }
    }
}
